<?php

namespace app\data\service;

use app\admin\service\FeiShuService;
use app\admin\service\WxWorkService;
use think\admin\Service;

/**
 * 品质问题
 * Class ReworkService
 * @package app\data\service
 */
class ReworkService extends Service
{
    /**
     * 格式化品质问题
     * @param $data
     * @return mixed
     */
    public function format($data)
    {
        if (empty($data)) {
            return [];
        }
        $answerA = explode('###', $data['answer_a']);
        $answerB = explode('###', $data['answer_b']);
        $answerC = explode('###', $data['answer_c']);
        $answerD = explode('###', $data['answer_d']);

        $data['answer_a'] = $answerA[0] ?? '';
        $data['answer_b'] = $answerB[0] ?? '';
        $data['answer_c'] = $answerC[0] ?? '';
        $data['answer_d'] = $answerD[0] ?? '';

        $data['answers'][] = $answerA[0] ?? '';
        $data['answers'][] = $answerB[0] ?? '';
        $data['answers'][] = $answerC[0] ?? '';
        $data['answers'][] = $answerD[0] ?? '';

        $data['is_answer_a'] = intval($answerA[1] ?? 0);
        $data['is_answer_b'] = intval($answerB[1] ?? 0);
        $data['is_answer_c'] = intval($answerC[1] ?? 0);
        $data['is_answer_d'] = intval($answerD[1] ?? 0);

        $data['is_answers'] = [];
        if ($data['is_answer_a']) {
            $data['is_answers'][] = 0;
        }
        if ($data['is_answer_b']) {
            $data['is_answers'][] = 1;
        }
        if ($data['is_answer_c']) {
            $data['is_answers'][] = 2;
        }
        if ($data['is_answer_d']) {
            $data['is_answers'][] = 3;
        }

        return $data;
    }

    /**
     * 保存题目
     * @param $data
     * @return mixed
     */
    public function formatSaveQuestion($data)
    {
        $fields = [
            'answer_a',
            'answer_b',
            'answer_c',
            'answer_d',
        ];

        foreach ($data['answers'] as $key => $answer) {
            if (!empty($answer)) {
                $data[$fields[$key]] = $answer . "###" . (in_array($key, $data['is_answers'] ?? []) ? 1 : 0);
            }
        }

        return $data;
    }

    /**
     * 保存
     * @param $record
     * @return int|string
     */
    public function saveReworkRecord($record, $data)
    {
        $isExist = $this->app->db->name('rework_record')->where('uuid_city', $record['uuid_city'])->where('record_id', $record['id'])->findOrEmpty();

        $openId = $this->app->db->name('work_user')->where('id', $record['uuid_city'])->value('open_id');
        if ($openId) {
            $plateNumber = $record['plate_number'];
            $messageText = "尊敬的师傅，由于您施工车辆({$plateNumber}）存在返工现象，您的账号，已被锁定，请登录飞信，点击查看品质要求准则，完成品质任务，解锁后方可再次接单。<a href='" . config('fei_shu.url') . '/admin.html?show=quality_requirements&show_id=1#/data/rework.rework_answer_question/qualityRequirements.html?show_id=1' . "'>解锁></a>";
            FeiShuService::instance()->sendMessageTextByUserid($openId, $messageText);
        }

        $saveData['reason'] = $data['reason'];
        $saveData['images'] = $data['images'];
        $saveData['uuid_city'] = $record['uuid_city'];
        $saveData['record_id'] = $record['id'];
        $saveData['plate_number'] = $record['plate_number'];

        if ($isExist) {
            $this->app->db->name('rework_record')->where('id', $isExist['id'])->update($saveData);
            return $isExist['id'];
        }

        return $this->app->db->name('rework_record')->insertGetId($saveData);
    }

    /**
     * 格式化品质原因
     * @param $data
     * @return mixed
     */
    public function formatRework($data)
    {
        $rework = $this->app->db->name('rework_record')->where('record_id', $data['id'])->order('id', 'desc')->limit(1)->findOrEmpty();
        $data['reason'] = $rework['reason'] ?? '';
        $data['images'] = $rework['images'] ?? '';
        return $data;
    }
}